package com.dnxh.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.dnxh.constant.CommonConstant;
import com.dnxh.constant.CustomResult;
import com.dnxh.util.EmailUtil;
import com.dnxh.util.RedisUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * The type Email controller.
 *
 * @Author 枳湫
 * @Time 2022 /4/6 16:15
 * @Describe Basic_jar
 */
@RestController
@RequestMapping("/email")
public class EmailController {
	
	@Resource
	private EmailUtil emailUtil;
	@Resource
	private RedisUtil redisUtil;
	
	/**
	 * 发送验证码
	 *
	 * @param email the email
	 * @param scope the scope
	 * @return the string
	 */
	@GetMapping("/send")
	public CustomResult sendVerifyEmail(@RequestParam("email") String email,
	                                    @RequestParam("scope") String scope){
		StringBuilder rs = new StringBuilder();
		for (int j = 0; j < 6; j++) {
			rs.append("123456789".charAt(RandomUtil.randomInt(0, 9)));
		}
		String code = rs.toString ();
		String key = CommonConstant.SEND_MESSAGE + scope + StrUtil.COLON + email;
		redisUtil.set (key,code,300);
		emailUtil.sendThymeleafEmail (email,"注册",code);
		return CustomResult.success ();
	}
}
